package org.korifey.ffbdd.api.samples.deprecated;

import java.util.HashSet;
import java.util.Set;

import org.korifey.ffbdd.api.deprecated.Bdd;
import org.korifey.ffbdd.samples.assign.Timetable;

public class AssignBdd {
	Timetable timetable;
	
	class ContextStorage {
		Bdd<Integer>[] consumer;
		int startVar;
		int varnum;
		
		public ContextStorage(Timetable t) {
			startVar = Bdd.getNextVar();
			consumer = new Bdd[t.getSize()];
			for (int i=0; i<consumer.length; i++) {
				consumer[i] = Bdd.makeConst(-1);
				consumer[i] = consumer[i].insertNewList(t.getConsumerResources(i));
			}
			varnum = Bdd.getNextVar() - startVar;
		}

		public void addIteration(int index) {
			for (int i=0; i<index; i++) {
				//consumer[index] = Bdd.cond(Bdd.notEq(consumer[i], consumer[index]), consumer[index], -1)
				consumer[index] = consumer[index].condNE(consumer[i], -1);
			}
		}

		public Set<int[]> getResult() {
			if (consumer[consumer.length - 1].isConst()) return new HashSet();
			else return null;
		}
		
	}
	
	Set<int[]> solves;
	
	public Set<int[]> solve(Timetable t) {
		timetable = t;
		
		ContextStorage s = new ContextStorage(timetable);
		for (int i=0; i<t.getSize(); i++) {
			s.addIteration(i);
		}
		
		solves = s.getResult();
		
		return solves;
	}
	
	public void inner(int cnum, int[] solve) {
		if (cnum == timetable.getSize()) {
			solves.add(solve);
			return;
		}
		cycle: for (Integer resnum: timetable.getConsumerResources(cnum)) {
			for (int i=0; i<cnum; i++) {
				if (resnum == solve[i]) continue cycle;
			}
			int[] nsolve = solve.clone();
			nsolve[cnum] = resnum;
			inner(cnum+1, nsolve);
		}
	}
}
